package leetcode.code2134;

import leetcode.IDebug;

public class Solution implements IDebug {

	public int minSwaps(int[] nums) {
		int len = nums.length;
		int size = 0;// 一共做少个1
		for (int i = 0; i < len; i++) {
			size += nums[i] == 1 ? 1 : 0;
		}
		int l = 0, r = 0;
		int cnt1 = 0;// 以size为窗口中1的数量
		while (r < size) {
			cnt1 += nums[r++] == 1 ? 1 : 0;
		}
		int max = cnt1;// 最多max个1
		while (l < len && r < len * 2) {// 环数组，在l不越界的情况下，r最大走两倍
			int index = r++ % len;
			cnt1 += nums[index] == 1 ? 1 : 0;// 进来一个1
			cnt1 -= nums[l++] == 1 ? 1 : 0;// 出去一个1
			if (cnt1 > max) {
				max = cnt1;
			}
		}
		return size - max;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
